package cn.itsource.petHome.org.controller;

import cn.itsource.petHome.org.domain.Shop;
import cn.itsource.petHome.org.query.ShopQuery;
import cn.itsource.petHome.org.service.IShopService;
import cn.itsource.petHome.utils.AjaxResult;
import cn.itsource.petHome.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@CrossOrigin
public class ShopController {
    @Autowired
    private IShopService service;

    @GetMapping
    public List<Shop> selectAll(){
        return service.loadAll();
    }

    @PostMapping
    public PageResult<Shop> queryByPage(@RequestBody ShopQuery q){

        return service.queryByPage(q);
    }

    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        service.delete(id);
        return AjaxResult.me();
    }

    @PutMapping
    public AjaxResult upOrAdd(@RequestBody Shop s){
        if(s.getId()==null){
            service.save(s);
        }else{
            service.update(s);
        }
        return AjaxResult.me();
    }

    @PatchMapping
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        service.batchDelete(ids);
        return AjaxResult.me();
    }
}
